/**
├── controllers
    └── find_all.js
*/
const db = require('../config/db')
// 引入Sequelize对象
const Sequelize = db.sequelize

class articleController {
  /**
   * 获取温度统计详情
   * @returns {Promise.<void>}
   */
  static async findAll(ctx) {
    try {
      let data = await Sequelize.query(
        "SELECT a.temperature as bs_ss_t, b.temperature as bs_yb_t, c.temperature as mj_ss_t,d.temperature as mj_yb_t, date_format(a.datetime, '%Y-%m-%d %H:%i:%s') as datetime FROM bs_ss as a, bs_yb as b, mj_ss as c, mj_yb as d where a.datetime = b.datetime and a.datetime = c.datetime and a.datetime = d.datetime"
      )
      ctx.response.status = 200
      ctx.body = {
        code: 200,
        msg: '查询成功',
        data
      }
    } catch (err) {
      ctx.response.status = 412
      ctx.body = {
        code: 412,
        msg: '查询失败',
        data
      }
    }
  }
  /**
   * 按时间获取温度统计详情
   * @returns {Promise.<void>}
   */
  static async findAllByTime(ctx) {
    try {
      let request = ctx.request
      let req_query = request.query
      console.log(req_query)
      let data = await Sequelize.query(
        "SELECT a.temperature as bs_ss_t, b.temperature as bs_yb_t, c.temperature as mj_ss_t,d.temperature as mj_yb_t, date_format(a.datetime, '%Y-%m-%d %H:%i:%s') as datetime FROM bs_ss as a, bs_yb as b, mj_ss as c, mj_yb as d where a.datetime = b.datetime and a.datetime = c.datetime and a.datetime = d.datetime " +
        "and a.datetime between '" + req_query.start + "' and '" + req_query.end + "'"
      )
      ctx.response.status = 200
      ctx.body = {
        code: 200,
        msg: '查询成功',
        data
      }
    } catch (err) {
      ctx.response.status = 412
      ctx.body = {
        code: 412,
        msg: '查询失败',
        data
      }
    }
  }
  /**
   * 获取湿度统计详情
   * @returns {Promise.<void>}
   */
  static async findHumidityAll(ctx) {
    try {
      let data = await Sequelize.query(
        "SELECT a.humidity as bs_ss_h, b.humidity as bs_yb_h, c.humidity as mj_ss_h,d.humidity as mj_yb_h, date_format(a.datetime, '%Y-%m-%d %H:%i:%s') as datetime FROM bs_ss as a, bs_yb as b, mj_ss as c, mj_yb as d where a.datetime = b.datetime and a.datetime = c.datetime and a.datetime = d.datetime"
      )
      ctx.response.status = 200
      ctx.body = {
        code: 200,
        msg: '查询成功',
        data
      }
    } catch (err) {
      ctx.response.status = 412
      ctx.body = {
        code: 412,
        msg: '查询失败',
        data
      }
    }
  }
  /**
   * 按时间获取湿度统计详情
   * @returns {Promise.<void>}
   */
  static async findHumidityAllByTime(ctx) {
    try {
      let request = ctx.request
      let req_query = request.query
      console.log(req_query)
      let data = await Sequelize.query(
        "SELECT a.humidity as bs_ss_h, b.humidity as bs_yb_h, c.humidity as mj_ss_h,d.humidity as mj_yb_h, date_format(a.datetime, '%Y-%m-%d %H:%i:%s') as datetime FROM bs_ss as a, bs_yb as b, mj_ss as c, mj_yb as d where a.datetime = b.datetime and a.datetime = c.datetime and a.datetime = d.datetime " +
        "and a.datetime between '" + req_query.start + "' and '" + req_query.end + "'"
      )
      ctx.response.status = 200
      ctx.body = {
        code: 200,
        msg: '查询成功',
        data
      }
    } catch (err) {
      ctx.response.status = 412
      ctx.body = {
        code: 412,
        msg: '查询失败',
        data
      }
    }
  }
}

module.exports = articleController